﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public sealed class WaitLoadAssetBundle : CustomYieldInstruction
    {
        private AssetBundleLoadTask abLoadTask;
        private string error;
        private WaitForSecondsRealtime waitTime;

        public WaitLoadAssetBundle() { }
        public WaitLoadAssetBundle(AssetBundleLoadTask task)
        {
            this.abLoadTask = task;
        }

        public WaitLoadAssetBundle(string err)
        {
            this.error = err;
        }

        /// <summary>
        /// 已下载字节数
        /// </summary>
        public ulong DownBytes
        {
            get
            {
                if (this.waitTime != null) return 0;
                if (this.abLoadTask == null) return 0;
                return this.abLoadTask.DownBytes;
            }
        }

        public ulong Size
        {
            get
            {
                if (this.waitTime != null) return 0;
                if (this.abLoadTask == null) return 0;
                return this.abLoadTask.Size;
            }
        }

        /// <summary>
        /// 当前下载进度
        /// </summary>
        public float Progress
        {
            get
            {
                if (this.waitTime != null) return 0;
                if (this.abLoadTask == null) return 0;
                return this.abLoadTask.DownProgress;

            }
        }

        public ulong DownSpeed
        {
            get
            {
                if (this.waitTime != null) return 0;
                if (this.abLoadTask == null) return 0;
                return this.abLoadTask.DownSpeed;

            }
        }

        public string Error
        {
            get
            {
                if(this.waitTime != null)
                {
                    return (!this.waitTime.keepWaiting) ? "Simulate update failed" : string.Empty;
                }
                if (!string.IsNullOrEmpty(this.error))
                {
                    return error;
                }
                if(this.abLoadTask != null)
                {
                    return abLoadTask.Error;
                }
                return null;
            }
        }

        public override bool keepWaiting
        {
            get
            {
                if (this.waitTime != null) return this.waitTime.keepWaiting;
                return this.Error == null && this.abLoadTask != null && this.abLoadTask.IsHaveRequest();
            }
        }
    }
}

